package com.newly.common.feign.handle;

import cn.hutool.http.ContentType;
import cn.hutool.json.JSONUtil;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.newly.common.core.util.R;
import com.newly.common.feign.ext.NewlySentinelInvocationHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author:Ys
 * @date: 2021年09月05日 22:19
 * @desc:
 */
public class NewlyUrlBlockHandler implements BlockExceptionHandler {

    private static Logger log = LoggerFactory.getLogger(NewlyUrlBlockHandler.class);
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
        log.error("sentinel 降级 资源名称{}", e.getRule().getResource(), e);

        response.setContentType(ContentType.JSON.toString());
        response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
        response.getWriter().print(R.error(e.getMessage()));
    }
}
